package com.hehui.partimejob.js;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.webkit.JavascriptInterface;

import com.hehui.partimejob.BaseActivity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.text.replacer.StrReplacer;


public class BaseJSInterface {
    private Map<String,Object> data;
    public Context context;
    public OnJSInterfaceListener listener;
    public final String TAG = getClass().getSimpleName();

    public BaseJSInterface(BaseActivity context) {
        this.context = context;
    }


    public void runInUIThread(OnJSInterfaceListener listener) {
        this.listener = listener;
    }

    protected void runOnUIThread() {
        if (listener != null) {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    listener.doSomething();
                }
            });
        }
    }

    public interface OnJSInterfaceListener {
        void doSomething();
    }

    @JavascriptInterface
    public void log(String msg) {
        Log.d(TAG, "log: ---->" + msg);
    }

    @JavascriptInterface
    public String getData(String key) {
       return String.valueOf(data.get(key));
    }

    public void setData(Map<String, Object> data) {
        this.data = data;
    }
}
